﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Threading;
using System.IO;

using Commander.Classes;
using Commander.Structures;

namespace Commander.Views
{
    /// <summary>
    /// Логика взаимодействия для Deletes.xaml
    /// </summary>
    public partial class Deletes : Window
    {
        private string Patch;
        private List<IOView> lst;
        private Thread del;
        private int Count;
        private int val;

        public delegate void delClose();

        public Deletes()
        {
            InitializeComponent();
        }

        public Deletes(ListOfFiles data)
        {
            InitializeComponent();
            Patch = data.Patch;
            lst = new List<IOView>();
            foreach (IOView item in data.Files)
            {
                lst.Add(item);
            }
            Count = CountFile();
            progressBar1.Minimum = 0;
            progressBar1.Maximum = Count;
            progressBar1.Value = 0;
            val = 0;
        }

        public int CountFile()
        {
            int count = 0;
            foreach (IOView a in lst)
            {
                if (a.Size == "<Папка>")
                {
                    count += Directory.GetFiles(Patch + a.Name, "*.*", SearchOption.AllDirectories).Length;
                }
                else
                {
                    count++;
                }
            }
            return count;
        }

        //public void SetValue(string patch,System.Collections.IList str)
        //{
        //    Patch = patch;
        //    lst = str;
        //}

        public bool DeleteFile(string FileName)
        {            
            try
            {
                if (!File.Exists(FileName)) return true;
                FileInfo fileinfo = new FileInfo(FileName);
                fileinfo.IsReadOnly = false;
                this.Dispatcher.BeginInvoke((Action)(() => 
                { 
                    label3.Content = FileName;
                    progressBar1.Value = ++val;
                    label2.Content = (int)val * 100 / Count + "%";
                }));
                fileinfo.Delete();
            }
            catch(Exception error) 
            {
                MessageBox.Show(error.ToString());
            }

            return !File.Exists(FileName);
        }

        public bool DeleteDir(string FolderName)
        {
            if (!Directory.Exists(FolderName))
            {
                return true;
            }
            foreach (string files in Directory.GetFiles(FolderName))
            {
                DeleteFile(files);
            }

            foreach (string folder in Directory.GetDirectories(FolderName))
            {
                try
                {
                    DeleteDir(folder);
                }
                catch { }
            }

            try
            {
                this.Dispatcher.BeginInvoke((Action)(() => 
                { 
                    label3.Content = FolderName;
                }));
                Directory.Delete(FolderName);
            }
            catch { }
            return !Directory.Exists(FolderName);
        }

        private void Delet()
        {
            string tmp;
            foreach (IOView item in lst)
            {
                tmp = Patch;
                if (item.Size == "<Папка>")
                {
                    if (item.Name != "..")
                    {
                        tmp += item.Name + @"\";
                        DeleteDir(tmp);
                    }
                }
                else
                {
                    tmp += item.Name + "." + item.Type;
                    DeleteFile(tmp);
                }
            }
            this.Dispatcher.BeginInvoke(new delClose(clos), System.Windows.Threading.DispatcherPriority.Background);
        }

        private void clos()
        {
            this.Close();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            del = new Thread(new ThreadStart(Delet));
            del.Start();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {

        }
    }
}
